package com.example.spring.task;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Demo01Login {

    public static void main(String[] args) throws Exception{

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();

        //验证用户是否正确
        if (!(username.length()>=5&&username.length()<=11)){
            System.out.println("用户不正确");

        }
        //查数据库，返回所有数据，匹配密码
        Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://master:3306/student",
                "root",
                "123456");

        String sql="select * from login where username=?";
        PreparedStatement statement = conn.prepareStatement(sql);

        statement.setString(1,username);

        ResultSet rs = statement.executeQuery();

        if (!rs.next()){
            System.out.println("用户不存在");
        }
        User user = new User(rs.getString("id"),
                rs.getString("username")
                , rs.getString("password")
        );

        if (!password.equals(user.getPassword())){
            System.out.println("密码不正确");
        }
        System.out.println("登录成功");


    }

}
